<?php

class AppController extends Controller {
    var $helpers = array(
                            'Html',
                            'Ajax',
                            'Javascript',
                            'Session',
                            'Form',
                            'Number',
                            'Text',
                            'Time');
    var $paginate = array('limit'=>'10', 'format'=>false);
    var $components = array(
                            'Auth'  =>array(
                                            'userModel'     => 'Usuario',
                                            'fields'        => array('username'=>'usuario','password'=>'password'),
                                            'loginAction'   => array('controller'=>'usuarios','action'=>'login'),
                                            'logoutAction'  => array('controller'=>'usuarios','action'=>'logout'),
                                            'loginRedirect' => array('controller'=>'usuarios','action'=>'login' ),
                                            'logoutRedirect' => array('controller'=>'pages','action'=>'home'),
    										'loginError'		=> "Datos de ingreso erroneos",
                                            'authError'     => 'No tienes permisos',

                            ),
                            'Email',
                            'Session',
                            'RequestHandler',
                            'DebugKit.Toolbar'
                            );


    function  beforeFilter() {
        if(!$this->Auth->user()){
            $this->Auth->allow(/*array('controller' => 'usuarios', 'action' => 'add')*/'*');
		}
        if($this->Auth->user()){
            $this->set('logged', $this->Auth->user());
        }
       	if(isset($this->params['prefix'])){
       		switch($this->params['prefix']){
	       		case "admin":
	       			if($this->Auth->user('tipo') != 'Administrador'){
	       				$this->Session->setFlash('No tiene permisos para entrar a esta pagina');
	       				$this->redirect(array('controller' => 'pages','action' => 'display', 'home'));
	       			}
	       				break;
	       		case "escolar":
	       			if($this->Auth->user('tipo') != 'Escolar'){
	       				$this->Session->setFlash('No tiene permisos para entrar a esta pagina');
	       				$this->redirect(array('controller' => 'pages','action' => 'display', 'home'));
	       			}
	       				break;
	       		case "universitario":
	       			if($this->Auth->user('tipo') != 'Universitario'){
	       				$this->Session->setFlash('No tiene permisos para entrar a esta pagina');
	       				$this->redirect(array('controller' => 'pages','action' => 'display', 'home'));
	       			}
	       				break;
       		}
       	}

    }

    function beforeRender(){
        if($this->RequestHandler->isAjax()){
            Configure::write('debug', 0);
            $this->header('Pragma: no-cache');
            $this->header('Cache-control: no-cache');
            $this->header("Expires: Mon, 26 jul 1997 05:00:00 GMT");
            $this->layout = 'ajax';
        }
    }
}
?>
